package Search;

import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author zouran
 * createDate：2023/9/10  14:53
 */


public class A {
    static final int DEGREE = 20;

    public static void main(String[] args) {

//        gdal.AllRegister();
        Dataset data = gdal.Open("D:\\Users\\13055\\Desktop\\mars_system\\mars_data\\slope_min_tif\\slope.tif", gdalconst.GA_ReadOnly);
        int imWidth = data.GetRasterXSize();
        int imHeight = data.GetRasterYSize();
        int imBands = data.getRasterCount();
        double[] imGeotrans = data.GetGeoTransform();
        String imProj = data.GetProjection();

        Band band = data.GetRasterBand(1);
        int[] marsMap = new int[imWidth * imHeight];
        band.ReadRaster(0, 0, imWidth, imHeight, marsMap);

        boolean[][] marsValid = new boolean[imHeight][imWidth];
        for (int i = 0; i < imHeight; i++) {
            for (int j = 0; j < imWidth; j++) {
                int index = i * imWidth + j;
                marsValid[i][j] = marsMap[index] < DEGREE;
            }
        }

        BufferedImage image = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_BYTE_GRAY);
        for (int i = 0; i < imHeight; i++) {
            for (int j = 0; j < imWidth; j++) {
                if (marsValid[i][j]) {
                    image.setRGB(j, i, 0xFFFFFF);
                } else {
                    image.setRGB(j, i, 0x000000);
                }
            }
        }

        File outputFile = new File("output.png");
        try {
            ImageIO.write(image, "png", outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}